<?php
namespace App\Models\Scripts;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class vadab7b701f23bb82014c8506d3dc784e extends Sysupdate
{
    protected $version_id ='hr';

    private $table_name = 'admin_users_extend';
    private $returnData = array('code'=>0,'message'=>'执行成功');

    public function upgrade()
    {
        $r_data = $this->_step_1();
        if(!$r_data){
            return $r_data;
        }
        $r_data = $this->_step_2();
        if(!$r_data){
            return $r_data;
        }
        return $this->returnData;
    }

    private function _step_1()
    {
        try {
            //检查是否完成
            $res = DB::table($this->table_name)->get();
            if($res){
                $this->returnData['code'] = 101;
                $this->returnData['message'] = 'table '.$this->table_name.' exist';
                return $this->returnData;
            }
        } catch(\Illuminate\Database\QueryException $e) {
            $errorInfo = json_decode(json_encode($e->errorInfo),true);
            if($errorInfo[0]!='42S02'){
                Log::info('table '.$this->table_name.' error:',array('errorinfo'=>json_encode($e)));
                $this->returnData['code'] = 102;
                $this->returnData['message'] = json_encode($e);
                return $this->returnData;
            }
            $sql = "CREATE TABLE `admin_users_extend` (".
                "  `admin_id` int(11) NOT NULL COMMENT 'admin_user_id',".
                "  `birth_date` timestamp NULL DEFAULT NULL COMMENT '出生日期',".
                "  `nation` varchar(50) DEFAULT NULL COMMENT '名族',".
                "  `age` varchar(10) DEFAULT NULL COMMENT '年龄',".
                "  `idcard_no` varchar(18) DEFAULT NULL COMMENT '身份证号码',".
                "  `highest_edu` varchar(50) DEFAULT NULL COMMENT '最高学历',".
                "  `degree` varchar(50) DEFAULT NULL COMMENT '学位',".
                "  `marital_status` tinyint(1) DEFAULT NULL COMMENT '0 未婚/1已婚/2 离异/3 丧偶',".
                "  `stature` varchar(3) DEFAULT NULL COMMENT '身高 单位cm',".
                "  `political_affiliation` varchar(50) DEFAULT NULL COMMENT '政治面貌',".
                "  `native_place` varchar(100) DEFAULT NULL COMMENT '籍贯',".
                "  `reg_permanent_place` varchar(100) DEFAULT NULL COMMENT '户口所在地',".
                "  `reg_permanent_type` tinyint(1) DEFAULT NULL COMMENT '户口类型',".
                "  `technical_title` varchar(100) DEFAULT NULL COMMENT '技术职称',".
                "  `social_security_no` varchar(50) DEFAULT NULL COMMENT '社保号',".
                "  `public_reserve_funds` varchar(50) DEFAULT NULL COMMENT '公积金账户',".
                "  `current_address` varchar(200) DEFAULT NULL COMMENT '当前住址',".
                "  `home_address` varchar(200) DEFAULT NULL COMMENT '家庭住址',".
                "  `mobile_phone` varchar(11) DEFAULT NULL COMMENT '手机号',".
                "  `tel_phone` varchar(12) DEFAULT NULL COMMENT '固定电话',".
                "  `binduser_name` varchar(50) DEFAULT NULL COMMENT '紧急联系人姓名',".
                "  `binduser_work_address` varchar(200) DEFAULT NULL COMMENT '紧急联系人工作地点',".
                "  `binduser_phone` varchar(12) DEFAULT NULL COMMENT '紧急联系人电话',".
                "  `education_history` text COMMENT '教育/培训经历 json',".
                "  `work_history` text COMMENT '工作经历json',".
                "  `foreigner_language` varchar(20) DEFAULT NULL COMMENT '外语语种',".
                "  `foreigner_language_status` varchar(20) DEFAULT NULL COMMENT '0 一般/1 熟练',".
                "  `computer_science_level` varchar(20) DEFAULT NULL COMMENT '计算机应用水平',".
                "  `skill_title` varchar(50) DEFAULT NULL COMMENT '技术/技能职称',".
                "  `cantonese_skill_status` varchar(10) DEFAULT NULL COMMENT '粤语 0 不会/1 会',".
                "  `certificate_info` varchar(50) DEFAULT NULL COMMENT '考试种类和等级',".
                "  `lastest_employer_name` varchar(50) DEFAULT NULL COMMENT '最近雇主/上司姓名',".
                "  `lastest_employer_job` varchar(50) DEFAULT NULL COMMENT '最近雇主/上司职务',".
                "  `lastest_employer_phone` varchar(12) DEFAULT NULL COMMENT '最近雇主/上司电话',".
                "  `family_info` text COMMENT '家庭成员信息',".
                "  `acquaintance_name` varchar(50) DEFAULT NULL COMMENT '公司熟人姓名 无则为空',".
                "  `acquaintance_department` varchar(20) DEFAULT NULL COMMENT '熟人部门',".
                "  `acquaintance_job` varchar(20) DEFAULT NULL COMMENT '熟人职务',".
                "  `acquaintance_relation` varchar(20) DEFAULT NULL COMMENT '熟人关系',".
                "  PRIMARY KEY (`admin_id`)".
                ") ENGINE=MyISAM DEFAULT CHARSET=utf8;";
            DB::statement($sql);
            return $this->returnData;
        }
        return $this->returnData;
    }

    private function _step_2()
    {
        $tmp_arr = [];
        $res = DB::table('admin_users')->select('id')->get();
        $res = json_decode(json_encode($res),true);
        foreach ($res as $key=>$value){
            $tmp_arr[] = $value['id'];
        }
        $tmp = [];
        foreach ($tmp_arr as $value){
            $tmp[] = ['admin_id'=>$value];
        }
        $res = DB::table($this->table_name)->insert($tmp);
        if(!$res){
            $this->returnData['code'] = 99;
            $this->returnData['message'] ='同步失败';
            return $this->returnData;
        }
        $this->returnData['message'] ='同步成功';
        return $this->returnData;
    }
}
